﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace shuju_jiange.Com
{

    public class DataList : BindingList<Data>
    {
        private bool isSorted;
        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;

        private Dictionary<int, int> _listAC = new Dictionary<int, int>();
        private Dictionary<int, int> _listBG = new Dictionary<int, int>();
        private Dictionary<int, int> _listBAC2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listBBG2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listAAC2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listABG2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listH = new Dictionary<int, int>();
        private Dictionary<int, int> _ListCha = new Dictionary<int, int>();
        private Dictionary<int, int> _listACH2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listBGH2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listBGH3 = new Dictionary<int, int>();
        private Dictionary<int, int> _listSum = new Dictionary<int, int>();

        private Dictionary<int, int> _listA1 = new Dictionary<int, int>();
        private Dictionary<int, int> _listA2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listA3 = new Dictionary<int, int>();
        private Dictionary<int, int> _listA4 = new Dictionary<int, int>();
        private Dictionary<int, int> _listA5 = new Dictionary<int, int>();
        private Dictionary<int, int> _listA6 = new Dictionary<int, int>();

        private Dictionary<int, int> _listB1 = new Dictionary<int, int>();
        private Dictionary<int, int> _listB2 = new Dictionary<int, int>();
        private Dictionary<int, int> _listB3 = new Dictionary<int, int>();
        private Dictionary<int, int> _listB4 = new Dictionary<int, int>();
        private Dictionary<int, int> _listB5 = new Dictionary<int, int>();
        private Dictionary<int, int> _listB6 = new Dictionary<int, int>();



        private string[] _sumByItems = new string[] { };

        private string[] _totalByItems = new string[] { };
        public Dictionary<int, int> ListAC { get { return _listAC; } }
        public Dictionary<int, int> ListBG { get { return _listBG; } }
        public Dictionary<int, int> ListBAC2 { get { return _listBAC2; } }
        public Dictionary<int, int> ListBBG2 { get { return _listBBG2; } }

        public Dictionary<int, int> ListAAC2 { get { return _listAAC2; } }
        public Dictionary<int, int> ListABG2 { get { return _listABG2; } }

        public Dictionary<int, int> ListA1 { get { return _listA1; } }
        public Dictionary<int, int> ListA2 { get { return _listA2; } }
        public Dictionary<int, int> ListA3 { get { return _listA3; } }
        public Dictionary<int, int> ListA4 { get { return _listA4; } }
        public Dictionary<int, int> ListA5 { get { return _listA5; } }
        public Dictionary<int, int> ListA6 { get { return _listA6; } }
        public Dictionary<int, int> ListB1 { get { return _listB1; } }
        public Dictionary<int, int> ListB2 { get { return _listB2; } }
        public Dictionary<int, int> ListB3 { get { return _listB3; } }
        public Dictionary<int, int> ListB4 { get { return _listB4; } }
        public Dictionary<int, int> ListB5 { get { return _listB5; } }
        public Dictionary<int, int> ListB6 { get { return _listB6; } }




        public Dictionary<int, int> ListH { get { return _listH; } }

        public Dictionary<int, int> ListCha { get { return _ListCha; } }
        
        public Dictionary<int, int> ListACH2 { get { return _listACH2; } }
        public Dictionary<int, int> ListBGH2 { get { return _listBGH2; } }
        public Dictionary<int, int> ListBGH3 { get { return _listBGH3; } }
        public Dictionary<int, int> ListSum { get { return _listSum; } }
        public DataList()
        {
        }

        protected override bool IsSortedCore
        {
            get { return isSorted; }
        }

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirection; }
        }

        protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortProperty; }
        }

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }

        public string[] SumByItems
        {
            get
            {
                return _sumByItems;
            }
            set
            {
                _sumByItems = (value == null) ? new string[] { } : value;
                foreach (Data data in this)
                {
                    _sumByItems = value;
                    data.SumByItems = _sumByItems;
                }
            }
        }
        public string[] TotalByItems
        {
            get
            {
                return _totalByItems;
            }
            set
            {
                _totalByItems = (value == null) ? new string[] { } : value;
                foreach (Data data in this)
                {
                    _totalByItems = value;
                    data.TotalByItems = _totalByItems;
                }
            }
        }

        public int CountingAverageInterval(bool isShowRepeatData)
        {
            int times = 0;
            int sum = 0;


            if (!isShowRepeatData)
            {
                foreach (Data data in this)
                {
                    times += data.Time - 1;
                    sum += (data.Time - 1) * data.Interval;
                }
            }
            else
            {
                List<string> srcDatas = new List<string>();
                foreach (Data data in this)
                {
                    if (srcDatas.Contains(data.SrcData)) continue;
                    times += data.Time - 1;
                    sum += (data.Time - 1) * data.Interval;
                    srcDatas.Add(data.SrcData);
                }
            }
            return (times == 0) ? 0 : sum / times;
        }
        protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List<Data> items = this.Items as List<Data>;

            if (items != null)
            {
                // 对所有列都使用相同的排序逻辑：先按当前列排序，相同时按占有率排序
                items.Sort((x, y) =>
                {
                    // 获取要排序的属性值
                    object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
                    object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);
                    
                    int compareResult;
                    
                    // 比较当前列的值
                    if (xValue is IComparable)
                    {
                        compareResult = ((IComparable)xValue).CompareTo(yValue);
                    }
                    else if (xValue.Equals(yValue))
                    {
                        compareResult = 0;
                    }
                    else
                    {
                        compareResult = xValue.ToString().CompareTo(yValue.ToString());
                    }
                    
                    // 如果当前列的值相同，则按占有率排序
                    if (compareResult == 0)
                    {
                        compareResult = x.Zhanyoulv.CompareTo(y.Zhanyoulv);
                    }
                    
                    // 根据排序方向返回结果
                    return direction == ListSortDirection.Ascending ? compareResult : -compareResult;
                });
                
                isSorted = true;
            }
            else
            {
                isSorted = false;
            }

            sortProperty = property;
            sortDirection = direction;

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override void RemoveSortCore()
        {
            isSorted = false;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
        //排序
        public void Sort(PropertyDescriptor property, ListSortDirection direction)
        {
            this.ApplySortCore(property, direction);
        }

        public DataList MergeSrcData()
        {
            Dictionary<string, Data> list = new Dictionary<string, Data>();
            DataList newList = new DataList();
            Data.Seed = 1;
            foreach (Data data in this)
            {
                if (!list.ContainsKey(data.SrcData)) { list.Add(data.SrcData, data); }
            }
            foreach (Data data in list.Values)
            {
                newList.Add(data.CopyData());
                newList[newList.Count - 1].OwerList = newList;
            }
            return newList;
        }
        /// <summary>
        /// 刷新AC、BG等数据
        /// </summary>
        public void RefreshListData()
        {
            this._listAC = new Dictionary<int, int>();
            this._listBG = new Dictionary<int, int>();
            this._listBAC2 = new Dictionary<int, int>();
            this._listBBG2 = new Dictionary<int, int>();
            this._listAAC2 = new Dictionary<int, int>();
            this._listABG2 = new Dictionary<int, int>();
            this._listH = new Dictionary<int, int>();
            this._listACH2 = new Dictionary<int, int>();
            this._listBGH2 = new Dictionary<int, int>();
            this._listBGH3 = new Dictionary<int, int>();
            this._listSum = new Dictionary<int, int>();

            this._listA1 = new Dictionary<int, int>();
            this._listA2 = new Dictionary<int, int>();
            this._listA3 = new Dictionary<int, int>();
            this._listA4 = new Dictionary<int, int>();
            this._listA5 = new Dictionary<int, int>();
            this._listA6 = new Dictionary<int, int>();

            this._listB1 = new Dictionary<int, int>();
            this._listB2 = new Dictionary<int, int>();
            this._listB3 = new Dictionary<int, int>();
            this._listB4 = new Dictionary<int, int>();
            this._listB5 = new Dictionary<int, int>();
            this._listB6 = new Dictionary<int, int>();

            foreach (Data data in this)
            {
                 // AC
                if (_listAC.ContainsKey(data.Ac))
                {
                    _listAC[data.Ac]++;
                }
                else
                {
                    _listAC.Add(data.Ac, 1);
                }
                // BG
                if (_listBG.ContainsKey(data.Bg))
                {
                    _listBG[data.Bg]++;
                }
                else
                {
                    _listBG.Add(data.Bg, 1);
                }
                // H
                if (_listH.ContainsKey(data.H))
                {
                    _listH[data.H]++;
                }
                else
                {
                    _listH.Add(data.H, 1);
                }



                // BAC2
                if (_listBAC2.ContainsKey(data.BAc2))
                {
                    _listBAC2[data.BAc2]++;
                }
                else
                {
                    _listBAC2.Add(data.BAc2, 1);
                }

                // BBG2
                if (_listBBG2.ContainsKey(data.BBg2))
                {
                    _listBBG2[data.BBg2]++;
                }
                else
                {
                    _listBBG2.Add(data.BBg2, 1);
                }

                // AAC2
                if (_listAAC2.ContainsKey(data.AAc2))
                {
                    _listAAC2[data.AAc2]++;
                }
                else
                {
                    _listAAC2.Add(data.AAc2, 1);
                }

                // ABG2
                if (_listABG2.ContainsKey(data.ABg2))
                {
                    _listABG2[data.ABg2]++;
                }
                else
                {
                    _listABG2.Add(data.ABg2, 1);
                }

                // ACH2
                if (_listACH2.ContainsKey(data.Ach2))
                {
                    _listACH2[data.Ach2]++;
                }
                else
                {
                    _listACH2.Add(data.Ach2, 1);
                }
                // BGH2
                if (_listBGH2.ContainsKey(data.Bgh2))
                {
                    _listBGH2[data.Bgh2]++;
                }
                else
                {
                    _listBGH2.Add(data.Bgh2, 1);
                }
                // BGH3
                if (_listBGH3.ContainsKey(data.Bgh3))
                {
                    _listBGH3[data.Bgh3]++;
                }
                else
                {
                    _listBGH3.Add(data.Bgh3, 1);
                }
                // Sum
                if (_listSum.ContainsKey(data.Sum))
                {
                    _listSum[data.Sum]++;
                }
                else
                {
                    _listSum.Add(data.Sum, 1);
                }

                //a1-b6
                if (_listA1.ContainsKey(data.A1)) { _listA1[data.A1]++; } else { _listA1.Add(data.A1, 1); }
                if (_listA2.ContainsKey(data.A2)) { _listA2[data.A2]++; } else { _listA2.Add(data.A2, 1); }
                if (_listA3.ContainsKey(data.A3)) { _listA3[data.A3]++; } else { _listA3.Add(data.A3, 1); }
                if (_listA4.ContainsKey(data.A4)) { _listA4[data.A4]++; } else { _listA4.Add(data.A4, 1); }
                if (_listA5.ContainsKey(data.A5)) { _listA5[data.A5]++; } else { _listA5.Add(data.A5, 1); }
                if (_listA6.ContainsKey(data.A6)) { _listA6[data.A6]++; } else { _listA6.Add(data.A6, 1); }

                if (_listB1.ContainsKey(data.B1)) { _listB1[data.B1]++; } else { _listB1.Add(data.B1, 1); }
                if (_listB2.ContainsKey(data.B2)) { _listB2[data.B2]++; } else { _listB2.Add(data.B2, 1); }
                if (_listB3.ContainsKey(data.B3)) { _listB3[data.B3]++; } else { _listB3.Add(data.B3, 1); }
                if (_listB4.ContainsKey(data.B4)) { _listB4[data.B4]++; } else { _listB4.Add(data.B4, 1); }
                if (_listB5.ContainsKey(data.B5)) { _listB5[data.B5]++; } else { _listB5.Add(data.B5, 1); }
                if (_listB6.ContainsKey(data.B6)) { _listB6[data.B6]++; } else { _listB6.Add(data.B6, 1); }



            }            
        }
        
        public void CountingInterval()
        {
            Dictionary<string, List<int>> list = new Dictionary<string, List<int>>();
            for (int i=0;i<this.Count;i++)
            {

                
                Data data = this[i];

                //if (data.SrcData == "156")
                //{
                //    var a = 1;

                //}

                if (!list.ContainsKey(data.SrcData)) { list.Add(data.SrcData, new List<int>(new int[] {i})); }
                else
                {
                    list[data.SrcData].Add(i);
                }
            }

          //  var a = list["156"];

            foreach (var keyValue in list)
            {
                int time = keyValue.Value.Count;
                int sum = 0;
                if (keyValue.Value.Count > 1)
                {
                    for (int j = 1; j < keyValue.Value.Count; j++)
                    {
                        sum += keyValue.Value[j] - keyValue.Value[j - 1];
                    }
                }
                int interval = (time > 1) ? sum / (time - 1) : keyValue.Value[0];
                keyValue.Value.Add(time);
                keyValue.Value.Add(interval);
            }
            foreach(Data data in this)
            {
                if (list.ContainsKey(data.SrcData))
                {
                    List<int> indexes = list[data.SrcData];
                    if (indexes.Count >= 2)
                    {

                        //if (data.SrcData == "156")
                        //{
                        //    var a = 1;

                        //}

                        data.Time = indexes[indexes.Count - 2];
                        data.Interval = indexes[indexes.Count - 1];
                        data.ChaZhi = this.Count- indexes[indexes.Count - 3];
                    }
                }
            }
        }
    }

    public class ObjectPropertyCompare<T> : IComparer<T>
    {
        private PropertyDescriptor property;
        private ListSortDirection direction;

        public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
        {
            this.property = property;
            this.direction = direction;
        }

        /// <summary>
        /// 比较方法
        /// </summary>
        /// <param name="x">相对属性x</param>
        /// <param name="y">相对属性y</param>
        /// <returns></returns>
        public int Compare(T x, T y)
        {
            object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
            object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);

            int returnValue;

            if (xValue is IComparable)
            {
                returnValue = ((IComparable)xValue).CompareTo(yValue);
            }
            else if (xValue.Equals(yValue))
            {
                returnValue = 0;
            }
            else
            {
                returnValue = xValue.ToString().CompareTo(yValue.ToString());
            }

            if (direction == ListSortDirection.Ascending)
            {
                return returnValue;
            }
            else
            {
                return returnValue * -1;
            }
        }

        public bool Equals(T xWord, T yWord)
        {
            return xWord.Equals(yWord);
        }

        public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }
    }
}